<?php
session_start();

$app_id ='151952778172446';
$app_secret ='c744514181a031c475db49188e7c0b94';

//include_once 'facebook.php';
include_once 'amifb.php';
$fb= new Facebook(array('appId'=>$app_id,'secret'=>$app_secret));

$action = $_GET['action'];

switch($action)
{
	case 'oauth_redirect':
			verifyOauth();
		break;
	case 'set_status':
		setStaus();
		break;
	default:
			getOauthed();
}

function getOauthed()
{
	global $fb;
	$params=array(
				'scope'=>'user_education_history,user_work_history,email,publish_stream',
				'redirect_uri' => 'http://demo.amiworks.co.in/fb/index.php?action=oauth_redirect'
				);
	/* $url ='https://graph.facebook.com/oauth/authorize?client_id='.$app_id.'&redirect_uri=http://demo.amiworks.co.in/fb/index.php?action=oauth_redirect&scope=user_education_history,user_work_history,email,publish_stream'; */
	$url=$fb->getLoginUrl($params);
	header('location:'.$url);
}

function verifyOauth()
{
	global $fb;
	$code= $_GET['code'];
	$params=array(
				'code'=>$code,
				'redirect_uri' => 'http://demo.amiworks.co.in/fb/index.php?action=oauth_redirect'
				);
	$data = $fb->verifyOauth($params);
	if($data)
	{
		$_SESSION['access_token'] = $fb->getAccessToken();
			//echo 'at='.$fb->getAccessToken();
			//exit;
		header('location:http://demo.amiworks.co.in/fb/index.php?action=set_status');
	}
	else
	{
		exit;
	}
}	
function setStaus()
{
	global $fb;
	$param = array();
	
	$fb->setAccessToken($_SESSION['access_token']);
	//$param['access_token'] = $_SESSION['access_token'];
	$param['message'] = 'Testing Facebook GRAPH API using Oauth '.date('Y-m-d H:i:s');
	//$data=$fb->api('/me',$param);
	/* $data=$fb->api('/me/feed','POST',$param);
	$data=$fb->api('/me','GET',$param);
	echo '<pre>';
	print_r($data);
	$data=$fb->api('/me/friends','GET',$param);
	
	print_r($data);
	
	foreach($data['data'] as $f)
	{
		print_r($f);
		echo 'name='.$f['name']."\r\n";
		$data= $fb->api('/'.$f['id'],'GET',$param);
		print_r($data);
	} */
	
	$data = $fb->getProfile();
	if($data === false)
	{
		$err = $this->getLastError();
		
		echo 'Failed : '.$err['message'];
		exit;
	}
	print_r($data);
	
	$data = $fb->postStatusMessage($param);
	
	var_dump($data);
	$param['picture']='http://amiworks.com/logo.png';
	$param['link']='http://amiworks.com/';
	$param['name']='AmiWorks Name';
	$param['caption']='AmiWorks';
	$param['description']='We Build Integrated Web Applications';
	
	$data = $fb->postToProfileFeed($param);
	
	var_dump($data);
	
	foreach($data['data'] as $f)
	{
		print_r($f);
		echo 'name='.$f['name']."\r\n";
		$data= $fb->api('/'.$f['id'],'GET',$param);
		print_r($data);
	}
	
}	
	
	/* $url="https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&redirect_uri=http://demo.amiworks.co.in/fb/index.php?action=oauth_redirect&client_secret=".$app_secret."&code=".$code; */
	
	/* $data=httpRequest($url,'','GET');
	var_dump($data); */


function httpRequest($url, $auth_header, $method, $body = NULL) {
    if (!$method) {
      $method = "GET";
    };

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header)); // Set the headers.

    if ($body) {
      curl_setopt($curl, CURLOPT_POST, 1);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
      curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header, "Content-Type: text/xml;charset=utf-8"));   
    }

    $data = curl_exec($curl);
    curl_close($curl);
    return $data; 
  }